const global = getApp().globalData;
const Api = global.Api;
const wxApi = global.wxApi;
const userManager = global.userManager;
const defaultSendText = "获取验证码";
const countDownTime = 60;

Page({
  data: {
    phonenum: '',
    smscode: '',
    canSendCode: false,
    isCountDown: false,
    sendCodeText: defaultSendText,
    optionType: 1,
    password: ''
  },
  onLoad() {
    wx.setNavigationBarTitle({
      title: '登录'
    });
  },
  onPhoneNumChange(e) {
    let phone = e.detail.value;
    let isCountDown = this.data.isCountDown;
    this.setData({
      canSendCode: (phone.length === 11) && !isCountDown
    })
    
    this.setData({
      phonenum: phone
    })
  },
  onSmsCodeChange(e) {
    let smscode = e.detail.value;
    this.setData({
      smscode: smscode
    })
  },
  onPassChange( e ) {
    let password = e.detail.value;
    this.setData({
      password: password
    })
  },
  onSwitchOption( e ) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      optionType: type
    });
  },
  getSmsCode() {
    let self = this;

    if ( !this.data.canSendCode ) return;

    let phone = this.data.phonenum;
    if ( !isPhone(phone) ) {
      wx.showToast({
        title: "手机号格式有误",
        icon: "none"
      }) 
    }
    Api.sendSmsCode( phone , (data, code) => {
      if ( code === 0 ) {
        wx.showToast({
          title: "发送成功",
          icon: "none"
        })
        self.setData({
          canSendCode: false,
          isCountDown: true
        })
        self.countDown(countDownTime, {
          every(time) {
            self.setData({
              sendCodeText: "重新发送(" + time + "s)"
            })
          },
          end() {
            self.setData({
              sendCodeText: defaultSendText,
              isCountDown: false
            })
            if (self.data.phonenum.length === 11) {
              self.setData({
                canSendCode: true
              })
            }
          }
        })
      }
    })
  },
  submit() {
    let type = this.data.optionType;
  
    if ( type == 1 ) {
      this.userBind();
    }
    if ( type == 2 ) {
      this.userLogin();
    }
  },
  userLogin() {
    let password = this.data.password;
    let phone = this.data.phonenum;
    if ( !phone || !password ) {
      wx.showToast({
        title: "输入内容不能为空哦",
        icon: 'none'
      })
      return;
    }
    if ( !isPhone(phone) ) {
      wx.showToast({
        title: "手机号格式有误",
        icon: "none"
      }) 
    }
    let params = {
      phone: phone,
      password: password
    }
    Api.normalLogin( params, res => {
      global.setUserInfo( res );
      wx.switchTab({
        url: '/pages/index/index'
      })
    } )
  },
  userBind() {
    // 以上为测试
    let params = {};
    let smsCode = this.data.smscode;
    let phone = this.data.phonenum;
    if ( !phone || !smsCode ) {
      wx.showToast({
        title: "输入内容不能为空哦",
        icon: 'none'
      })
      return;
    }
    if ( !isPhone(phone) ) {
      wx.showToast({
        title: "手机号格式有误",
        icon: "none"
      }) 
    }
    userManager.getAuthInfo( data => {
      if ( data ) {
        params = {
          authCode: data.authCode,
          encryptedData: data.encryptedData,
          iv: data.iv,
          phone: phone,
          smsCode: smsCode,
          sysType: '3'
        }
        Api.userBind(params, (res, code) => {
          if ( code != 0 ) {
            return ;
          }
          if ( res ) {
            global.setUserInfo( res );
          }
          wx.showToast({
            title: "登录成功"
          })
          setTimeout(() => {
            wxApi.navigateTo('../../mine/user-info/user-info', {
              type: 'bind'
            })
          }, 500);
          // wx.switchTab({
          //   url: '/pages/index/index'
          // })
          console.log(res);
        })
        return;
      } 
      console.log('登录失败....')
    } )
  },
  getWXUserInfo() {
    
  },
  
  countDown( time, params) {
    let interval = setInterval(() => {
      if (time === -1) {
        params.end && params.end()
        clearInterval(interval);
        return;
      }
      params.every && params.every(time);
      time --
    }, 1000);
  }
}) 
function isPhone (value) {
  var reg = /^((1[3-8][0-9])+\d{8})$/;
  return reg.test(value);
}